package com.xyrl.project.app;

import com.xyrl.common.util.ObjectUtil;
import com.xyrl.project.common.authentication.JWTToken;
import com.xyrl.project.common.authentication.JWTUtil;
import com.xyrl.project.common.authentication.LoginInfo;
import com.xyrl.project.common.base.service.CacheService;
import com.xyrl.project.common.constant.XyrlConstant;
import com.xyrl.project.common.csr.entity.CsrUser;
import com.xyrl.project.common.manager.UserManager;
import com.xyrl.project.common.system.entity.User;
import com.xyrl.project.common.util.XyrlUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.mgt.SecurityManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.mock.web.MockHttpSession;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import org.testng.annotations.BeforeClass;

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class BaseMock extends AbstractTestNGSpringContextTests {
    @Autowired
    public WebApplicationContext wac;
    public MockMvc mockMvc;
    public MockHttpSession session;
    public MockHttpServletRequest request;
    public MockHttpServletResponse response;
    @Value("${xyrl.shiro.close.auth.default.user:mrbird}")
    private String defaultUser;
    @Value("${xyrl.shiro.close.auth.default.userType:system}")
    private String defaultUserType;
    @Autowired
    private SecurityManager securityManager;
    @Autowired
    CacheService cacheService;
    @Autowired
    UserManager userManager;

    @BeforeClass
    public void setup() {
        System.out.println("--------------BeforeClass");
        mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
        response = new MockHttpServletResponse();
        request = new MockHttpServletRequest(wac.getServletContext());
        session = new MockHttpSession(wac.getServletContext());
        request.setSession(session);
        //模拟登录用户
        LoginInfo info = new LoginInfo(defaultUser, defaultUserType);
        //用户密码（后端或营业员登录）/用户手机号客户登录
        String token = "";

        if (defaultUserType.equalsIgnoreCase(XyrlConstant.USER_TYPE_SYSTEM) || defaultUserType.equalsIgnoreCase(XyrlConstant.USER_TYPE_SALESMAN)) {
            User user = userManager.getUser(defaultUser);
            if (ObjectUtil.isEmpty(user)) {
                throw new RuntimeException("用户[" + defaultUser + "],用户类型[" + defaultUserType + "]不存");
            }
            token = JWTUtil.sign(info, user.getPassword());
        } else {
            CsrUser csrUser = userManager.getCustomer(defaultUser);
            if (ObjectUtil.isEmpty(csrUser)) {
                throw new RuntimeException("用户[" + defaultUser + "],用户类型[" + defaultUserType + "]不存");
            }
            token = JWTUtil.sign(info, csrUser.getMobile());
        }
        token = XyrlUtil.encryptToken(token);
        JWTToken jwtToken = new JWTToken(XyrlUtil.decryptToken(token));
        //缓存用户登录信息
        cacheService.cacheLoginUser("192.168.1.62", info, token);
        SecurityUtils.setSecurityManager(securityManager);
        //设置登录用户
        SecurityUtils.getSubject().login(jwtToken);
    }

}
